package com.ruoyi.factory.buyprice.service.impl;

import java.util.List;

import com.ruoyi.factory.buyprice.domain.BumawhHead;
import com.ruoyi.factory.buyprice.domain.BumawhRawBody;
import com.ruoyi.factory.buyprice.mapper.BumawhHeadMapper;
import com.ruoyi.factory.buyprice.service.IBumawhHeadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购行情wh头Service业务层处理
 * 
 * @author ljj
 * @date 2024-06-11
 */
@Service
public class BumawhHeadServiceImpl implements IBumawhHeadService
{
    @Autowired
    private BumawhHeadMapper bumawhHeadMapper;

    /**
     * 查询采购行情wh头
     * 
     * @param id 采购行情wh头主键
     * @return 采购行情wh头
     */
    @Override
    public BumawhHead selectBumawhHeadById(Long id)
    {
        return bumawhHeadMapper.selectBumawhHeadById(id);
    }

    /**
     * 查询采购行情wh头列表
     * 
     * @param bumawhHead 采购行情wh头
     * @return 采购行情wh头
     */
    @Override
    public List<BumawhHead> selectBumawhHeadList(BumawhHead bumawhHead)
    {
        return bumawhHeadMapper.selectBumawhHeadList(bumawhHead);
    }

    /**
     * 新增采购行情wh头
     * 
     * @param bumawhHead 采购行情wh头
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBumawhHead(BumawhHead bumawhHead)
    {
        int rows = bumawhHeadMapper.insertBumawhHead(bumawhHead);
        insertBumawhRawBody(bumawhHead);
        return rows;
    }

    /**
     * 修改采购行情wh头
     * 
     * @param bumawhHead 采购行情wh头
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBumawhHead(BumawhHead bumawhHead)
    {
        bumawhHeadMapper.deleteBumawhRawBodyByBumaHeadId(bumawhHead.getId());
        insertBumawhRawBody(bumawhHead);
        return bumawhHeadMapper.updateBumawhHead(bumawhHead);
    }

    /**
     * 批量删除采购行情wh头
     * 
     * @param ids 需要删除的采购行情wh头主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBumawhHeadByIds(Long[] ids)
    {
        bumawhHeadMapper.deleteBumawhRawBodyByBumaHeadIds(ids);
        return bumawhHeadMapper.deleteBumawhHeadByIds(ids);
    }

    /**
     * 删除采购行情wh头信息
     * 
     * @param id 采购行情wh头主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBumawhHeadById(Long id)
    {
        bumawhHeadMapper.deleteBumawhRawBodyByBumaHeadId(id);
        return bumawhHeadMapper.deleteBumawhHeadById(id);
    }

    /**
     * 新增采购行情wh上游物料信息
     * 
     * @param bumawhHead 采购行情wh头对象
     */
    public void insertBumawhRawBody(BumawhHead bumawhHead)
    {
        List<BumawhRawBody> bumawhRawBodyList = bumawhHead.getBumawhRawBodyList();
        Long id = bumawhHead.getId();
        if (StringUtils.isNotNull(bumawhRawBodyList))
        {
            List<BumawhRawBody> list = new ArrayList<BumawhRawBody>();
            for (BumawhRawBody bumawhRawBody : bumawhRawBodyList)
            {
                bumawhRawBody.setBumaHeadId(id);
                list.add(bumawhRawBody);
            }
            if (list.size() > 0)
            {
                bumawhHeadMapper.batchBumawhRawBody(list);
            }
        }
    }
}
